package com.common.library.ui.activity;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import com.common.library.AppManager;
import com.common.library.R;
import com.common.library.ui.view.LoadingScaleView;


/**
 * @author txf
 * @Title 基类
 * @package com.txf.prevention.ui.activity
 * @date 2017/5/6 0006
 */
public class BaseActivity extends AppCompatActivity {
    private LoadingScaleView mLoadingScaleView;
    protected  String TAG = getClass().getName();

    /**
     * 隐藏导航栏
     */
    protected void hideNavigationBar() {
        if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) {
            View v = this.getWindow().getDecorView();
            v.setSystemUiVisibility(View.GONE);
        } else if (Build.VERSION.SDK_INT >= 19) {
            View decorView = getWindow().getDecorView();
            int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(uiOptions);
        }
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AppManager.getInstance().addActivity(this);
    }
    public <E extends View> E getViewById(@IdRes int id) {
        return (E) findViewById(id);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        AppManager.getInstance().delActivity(this);
    }

    /**
     * 重写 setContentView() 添加loading层
     */
    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        View view = LayoutInflater.from(this).inflate(R.layout.activity_base, null);
        FrameLayout base_activity_content = (FrameLayout) view.findViewById(R.id.base_activity_content);
        View childview = LayoutInflater.from(this).inflate(layoutResID, null);
        base_activity_content.addView(childview, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
        base_activity_content.addView(buildLoadingView(), new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
        super.setContentView(view);
    }
    protected View buildLoadingView() {
        mLoadingScaleView = new LoadingScaleView(this);
        mLoadingScaleView.InterceptClick(true);
        hideLoadingView();
        return mLoadingScaleView;
    }

    protected void showLoadingView() {
        if (mLoadingScaleView.getVisibility() != View.VISIBLE)
            mLoadingScaleView.setVisibility(View.VISIBLE);
    }

    protected void hideLoadingView() {
        if (mLoadingScaleView.getVisibility() == View.VISIBLE)
            mLoadingScaleView.setVisibility(View.INVISIBLE);
    }

}
